/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ita.escolaita.view;

import br.ita.escolaita.ejb.DisciplinaEjbLocal;
import br.ita.escolaita.model.entity.Disciplina;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Rafael
 */
public class DisciplinaBean {

    @EJB
    private DisciplinaEjbLocal disciplinaEjb;
    private Disciplina disciplina = new Disciplina();
    private List<Disciplina> disciplinas;

    public DisciplinaBean() {
    }

    public String salvar() {
        disciplinaEjb.salvar(disciplina);
        disciplina = new Disciplina();
        return "sucesso";
    }

    public String voltar() {
        HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ses.removeAttribute("disciplinaBean");
        ses.removeAttribute("serieBean");
        ses.removeAttribute("disciplina");
        return "voltar";
    }

    public String pesquisar() {
        String retorno = null;
        try {
            disciplinas = disciplinaEjb.listar(disciplina);
            retorno = "sucesso";
        } catch (Exception e) {
            e.printStackTrace();
            retorno = "erro";
        }

        return retorno;
    }

    public String todas() {
        String retorno = null;
        try {
            disciplinas = disciplinaEjb.listarTodas();
            retorno = "sucesso";
        } catch (Exception e) {
            e.printStackTrace();
            retorno = "erro";
        }

        return retorno;
    }

    public void selecionarListener(ActionEvent event) {
        UIParameter component = (UIParameter) event.getComponent().findComponent("disciplinaDesativar");
        if (component == null){
            component = (UIParameter) event.getComponent().findComponent("disciplinaAtivar");
        }
        if (component == null){
            component = (UIParameter) event.getComponent().findComponent("disciplinaAlterar");
        }
        if (component != null){
            disciplina = (Disciplina) component.getValue();
        }
    }

    public void desativar() {
        disciplina.setStatus('I');
        disciplinaEjb.alterar(disciplina);
        disciplina = new Disciplina();
    }

    public void ativar() {
        disciplina.setStatus('A');
        disciplinaEjb.alterar(disciplina);
        disciplina = new Disciplina();
    }

    public String alterar() {
        return "alterar";
    }

    public String salvarAlteracao() {
        disciplinaEjb.alterar(disciplina);
        disciplina = new Disciplina();
        return "sucesso";
    }

    // Getters and Setters
    public Disciplina getDisciplina() {
        return disciplina;
    }

    public void setDisciplina(Disciplina disciplina) {
        this.disciplina = disciplina;
    }

    public List<Disciplina> getDisciplinas() {
        return disciplinas;
    }

    public void setDisciplinas(List<Disciplina> disciplinas) {
        this.disciplinas = disciplinas;
    }
}
